
#include "mymainwindow.h"
#include "openglframe.h"
#include "typedefs.h"

#include <iostream>

using namespace std;

void printInfoAndExit() {
   
   cout<<endl;
   cout<<"Usage: qtMocapViewer filename1.{asf} filename2.{amc}"<<endl;
   exit(0);
}

/// Constructor.
MyMainWindow::MyMainWindow(QMainWindow *parent): QMainWindow(parent) {
   
   // Make sure the default opengl format includes stencil buffer and alpha.
   QGLFormat fmt;
   fmt.setAlpha(true);
   fmt.setStencil(true);
   QGLFormat::setDefaultFormat(fmt);
   
   // Executes the method which builds the remaining widgets of the window.
   // (Inherited from Ui::MainWindow)
   setupUi (this);
   
   timer = new QTimer(this);
   
   connect(actionZoomIn, SIGNAL(triggered()), this->glFrame, SLOT(zoomIn()));
   connect(actionZoomOut, SIGNAL(triggered()), this->glFrame, SLOT(zoomOut()));
   connect(actionZoomAll, SIGNAL(triggered()), this->glFrame, SLOT(zoomAll()));
   
   connect(slider, SIGNAL(sliderPressed()), this, SLOT(setMaximumValueSlider()));
   connect(slider, SIGNAL(valueChanged(int)), this, SLOT(animateValueSlider(int)));
   
   //~ // groupping exclusives actions 
   meshrenderGroup = new QActionGroup(this);
   meshrenderGroup->addAction(actionRenderPoints);
   meshrenderGroup->addAction(actionRenderWireframe);
   meshrenderGroup->addAction(actionRenderHiddenLines);
   meshrenderGroup->addAction(actionRenderFlatLines);
   meshrenderGroup->addAction(actionRenderFlat);
   meshrenderGroup->addAction(actionRenderSmooth);
}

void MyMainWindow::open(int argc, char *argv[]) {
   
   if (argc != 3) printInfoAndExit();
   
   QString filename1(argv[1]);
   QString filename2(argv[2]);
   if (filename1 != "" and filename2 != "") {
      QFileInfo fi1(filename1);
      QString extension1 = fi1.suffix();
      if (extension1 != "asf") {
         printInfoAndExit();
         return;
      }
      
      QFileInfo fi2(filename2);
      QString extension2 = fi2.suffix();
      if (extension2 != "amc") {
         printInfoAndExit();
         return;
      }
      
      if (not glFrame->loadSkeleton(filename1)) {
         QString errorMsgFormat = "xError encountered while opening file:\n\"%1\"\n\nError details: The \"%2\" format is incorrect";
         QMessageBox::critical(this, tr("Opening Error"), errorMsgFormat.arg(filename1, extension1));
         return;
      }
      
      if (not glFrame->loadMotion(filename2)) {
         QString errorMsgFormat = "xError encountered while opening file:\n\"%1\"\n\nError details: The \"%2\" format is incorrect";
         QMessageBox::critical(this, tr("Opening Error"), errorMsgFormat.arg(filename2, extension2));
         return;
      }
      
      //filenamemodel = filename;
      //this->setWindowTitle("qtMocapViewer - " + filenamemodel);
   }
}

/// Automatically bound to the actionOpen menu item of the main window 
/// by the designer mechanism. Pops up a file open dialog for selecting
/// an "off" file.
void MyMainWindow::on_actionOpen_triggered() 
{
}

void MyMainWindow::on_actionRenderPoints_triggered() {
   glFrame->meshRenderMode(POINTS);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderWireframe_triggered() {
   glFrame->meshRenderMode(WIREFRAME);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderFlat_triggered() {
   glFrame->meshRenderMode(FLAT);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderFlatLines_triggered() {
   glFrame->meshRenderMode(FLATLINES);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderHiddenLines_triggered() {
   glFrame->meshRenderMode(HIDDENLINES);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderSmooth_triggered() {
   glFrame->meshRenderMode(SMOOTH);
   glFrame->updateGL();
}

void MyMainWindow::on_actionLightOnOff_triggered() {
   if (actionLightOnOff->isChecked())
      actionLightOnOff->setIcon(QIcon(QString::fromUtf8("icons/lighton.png")));
   else 
      actionLightOnOff->setIcon(QIcon(QString::fromUtf8("icons/lightoff.png")));
   glFrame->lightingOnOff(actionLightOnOff->isChecked());
   glFrame->updateGL();
}

void MyMainWindow::on_actionProjection_triggered() {
   
   if (glFrame->isProjectionOrtho()) {
      const QString qstring("Perspective");
      actionProjection->setText(qstring);
      actionProjection->setIcon(QIcon(QString::fromUtf8("icons/perspective.png")));
      glFrame->orthographic(false);
   }
   else {
      const QString qstring("Orthographic");
      actionProjection->setText(qstring);
      actionProjection->setIcon(QIcon(QString::fromUtf8("icons/orthographic.png")));
      glFrame->orthographic(true);
   }
   
   glFrame->setGLview();
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewModel_triggered() {
   
   if (actionViewModel->isChecked())
      glFrame->viewItemOn(MODELDEF);
   else 
      glFrame->viewItemOff(MODELDEF);
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewFloor_triggered() {
   
   if (actionViewFloor->isChecked())
      glFrame->viewItemOn(FLOOR);
   else 
      glFrame->viewItemOff(FLOOR);
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewInfo_triggered() {
   
   if (actionViewInfo->isChecked())
      glFrame->viewItemOn(INFO);
   else 
      glFrame->viewItemOff(INFO);
   glFrame->updateGL();
}

void MyMainWindow::setMaximumValueSlider() {
   
   slider->setMaximum(glFrame->nMaxofFrames());
}

void MyMainWindow::animateValueSlider(int val) {
   
   glFrame->animateFrame(val-1);
}

void MyMainWindow::addValueSlider() {
   
   if (slider->value() < slider->maximum())
      slider->setValue(slider->value()+1);
   else
      on_bStop_clicked();
}

void MyMainWindow::on_bPlay_clicked() {
   
   slider->setMaximum(glFrame->nMaxofFrames());
   
   connect(timer, SIGNAL(timeout()), this, SLOT(addValueSlider()));
   timer->start(0);
}

void MyMainWindow::on_bStop_clicked() {
   
   slider->setValue(0);
   disconnect(timer, 0, 0, 0);
}

void MyMainWindow::on_bPause_clicked() {
   
   disconnect(timer, 0, 0, 0);
}
